package com.cooperate.fly.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

public class ZipUtil {
	public static boolean zipFiles(String filePath, String descDir) {
		ZipOutputStream zos = null;

		try {
			zos = new ZipOutputStream(new FileOutputStream(descDir));
			// zos.setEncoding(System.getProperty("sun.jnu.encoding"));
			zos.setEncoding("GBK");
			startZip(zos, "", filePath);
		} catch (IOException e) {
			File zipFile = new File(descDir);

			if (zipFile.exists()) {
				zipFile.delete();
			}

			e.printStackTrace();
		} finally {
			try {
				if (zos != null) {
					zos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	private static void startZip(ZipOutputStream zos, String oppositePath,
			String directory) throws IOException {
		File file = new File(directory);

		if (file.isDirectory()) {
			File[] files = file.listFiles();

			for (int i = 0; i < files.length; i++) {
				File aFile = files[i];

				if (aFile.isDirectory()) {
					String newOppositePath = oppositePath + aFile.getName() + "/";
					ZipEntry entry = new ZipEntry(oppositePath + aFile.getName() + "/");
					zos.putNextEntry(entry);
					zos.closeEntry();
					startZip(zos, newOppositePath, aFile.getPath());
				} else {
					zipFile(zos, oppositePath, aFile);
				}
			}
		} else {
			zipFile(zos, oppositePath, file);
		}
	}

	private static void zipFile(ZipOutputStream zos, String oppositePath,
			File file) {
		InputStream is = null;

		try {
			ZipEntry entry = new ZipEntry(oppositePath + file.getName());
			zos.putNextEntry(entry);
			is = new FileInputStream(file);
			int length = 0;
			int bufferSize = 1024;
			byte[] buffer = new byte[bufferSize];
			while ((length = is.read(buffer, 0, bufferSize)) >= 0) {
				zos.write(buffer, 0, length);
			}
			zos.closeEntry();
		} catch (IOException ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}

	public static void unZipFiles(String zipFilePath, String descDir) {
		// System.setProperty("sun.zip.encoding",
		// System.getProperty("sun.jnu.encoding"));

		File zipFile = new File(zipFilePath);

		File pathFile = new File(descDir);

		if (!pathFile.exists()) {
			pathFile.mkdirs();
		}
		ZipFile zip = null;

		InputStream in = null;

		OutputStream out = null;

		try {
			zip = new ZipFile(zipFile, "GBK");

			for (Enumeration<? extends ZipEntry> entries = zip.getEntries(); entries
					.hasMoreElements();) {
				ZipEntry entry = entries.nextElement();

				String zipEntryName = entry.getName();

				in = zip.getInputStream(entry);

				String outPath = (descDir + "/" + zipEntryName).replaceAll(
						"\\*", "/");

				File file = new File(outPath.substring(0,
						outPath.lastIndexOf('/')));

				if (!file.exists()) {
					file.mkdirs();
				}

				if (new File(outPath).isDirectory()) {
					continue;
				}
				out = new FileOutputStream(outPath);

				byte[] buf1 = new byte[4 * 1024];

				int len;

				while ((len = in.read(buf1)) > 0) {
					out.write(buf1, 0, len);
				}
				in.close();

				out.close();
			}
		} catch (IOException e) {
			pathFile.delete();
			e.printStackTrace();
		} finally {
			try {
				if (zip != null) {
					zip.close();
				}
				if (in != null) {
					in.close();
				}
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
